#include "scene_1.h"
#include "cocos-ext.h"
#include "Over.h"


//#pragma execution_character_set("utf-8")

using namespace cocos2d::extension;

CCScene* scene_1::scene()
{
	CCScene*scene = CCScene::create();
	scene_1*layer = scene_1::create();
	scene->addChild(layer);
	return scene;
}


void scene_1::move_plane()
{

	if ((GetKeyState(VK_RIGHT)) < 0)
	{
		if (plane_xy.x <= 452 - 35)
		{
			CCMoveBy *go = CCMoveBy::create(0.05f, ccp(10, 0));
			plane->runAction(go);
			plane_xy.x += 10;
		}

	}


	else if (GetKeyState(VK_LEFT) < 0)
	{
		if (plane_xy.x >= 10 + 35)
		{
			CCMoveBy *go = CCMoveBy::create(0.05f, ccp(-10, 0));
			plane->runAction(go);
			plane_xy.x -= 10;

		}
	}



	else if (GetKeyState(VK_DOWN) < 0)
	{
		if (plane_xy.y >= 10 + 24)
		{
			CCMoveBy *go = CCMoveBy::create(0.05f, ccp(0, -10));
			plane->runAction(go);
			plane_xy.y -= 10;
		}

	}



	else if (GetKeyState(VK_UP) < 0)
	{
		if (plane_xy.y <= 681 - 24)
		{
			CCMoveBy *go = CCMoveBy::create(0.05f, ccp(0, 10));
			plane->runAction(go);
			plane_xy.y += 10;
		}
	}

}

unsigned char aa = 0;
unsigned char ii = 0;
int load_plane_time = 0;

void scene_1::update(float a)   /////     单次  loop
{
	ii++;
	i++;

	score_++;
	sprintf(str_1, "%d", score_);
	score->setString(str_1);


	if (boss_mode == true)
	{
		boss_move();
		boss_judge();


	}
	else
	{

		enemy_load();
		enemy_move();  //控制敌机的 移动 渲染
	//	enemy_bullet_move();  // 控制敌机子弹 的移动和渲染
		enemy_juge();  // 敌人子弹 和敌机 的规则
	}


	if (isover == true)
	{
		load_plane_time++;
		if (load_plane_time>=119)
		{
			isover = false;
			load_plane_time = 0;
			plane_xy.x = size.width / 2;
			plane_xy.y = 150;
		}
	
	}
	else
	{
		move_plane();//移动飞机
		if (ii > 11)
		if (GetKeyState(VK_SPACE) < 0)
		{
			ii = 0;
			CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("data\\planeshoot0.mp3");

			for (int iaa = 0; iaa < 6; iaa++)   //  子弹位置
			{
				if (bullet_xy_exit[iaa] == false)  // 不存在子弹
				{

					bullet[iaa] = CCSprite::create("data\\bullet.png");///////////////////////////////////////
					this->addChild(bullet[iaa]);
					bullet[iaa]->setPosition(ccp(plane_xy.x, plane_xy.y + 35));
					bullet_xy[iaa] = ccp(plane_xy.x, plane_xy.y + 35);
					bullet_xy_exit[iaa] = true;
					break;
				}


			}


	}



	}

	for (int ia = 0; ia < 6; ia++)   //  子弹位置
	{
		if (bullet_xy_exit[ia] == true)  // 存在子弹
		{

			bullet_xy[ia].y += 12.8f;
			bullet[ia]->setPosition(bullet_xy[ia]);
			scene_1::bullet_judge();
			if (bullet_xy[ia].y>730) //子弹超界
			{
				bullet_xy_exit[ia] = false;
				this->removeChild(bullet[ia]);
				bullet[ia] = NULL;
			}
		}

	}
}



bool scene_1::init()
{
	k = time(NULL);			//以时间为种子 
	srand(k);
	i = 0;
	score_ = 0;
	isover = true;

	this->scheduleUpdate();
	this->setTouchEnabled(false);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("data\\gamebg0.mp3", true);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("data\\planeshoot0.mp3");
	enenmy_allno = 0;

	for (int iii = 0; iii < 6; iii++)
	{
		bullet_xy_exit[iii] = false;
	}

	score_kill = 0;
	CCSprite*bg;
	switch (rand() % 6)
	{
	case 0:
		bg = CCSprite::create("data\\bg1.jpg");
		break;

	case 1:
		bg = CCSprite::create("data\\bg2.jpg");
		break;


	case 2:
		bg = CCSprite::create("data\\bg3.jpg");
		break;


	case 3:
		bg = CCSprite::create("data\\bg4.jpg");
		break;


	case 4:
		bg = CCSprite::create("data\\bg5.jpg");
		break;

	default:
		bg = CCSprite::create("data\\default.png");
		break;



	}
	this->addChild(bg);
	bg->setPosition(ccp(size.width / 2, size.height / 2));

	load_plane();

	//////////////////////////////  运行界面 按钮菜单
	CCMenuItemFont *close = CCMenuItemFont::create("menu", this, menu_selector(scene_1::close));//这种菜单就不说了  
	close->setPosition(ccp(size.width - 41, size.height - 20));
	this->addChild(close);
	CCMenu* pMenu = CCMenu::create(close, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);

	/*
	CCMenuItemFont *set = CCMenuItemFont::create("Set", this, menu_selector(scene_1::close));//这种菜单就不说了
	set->setPosition(ccp(0 + 33, size.height - 20));
	this->addChild(set);
	CCMenu* pMenu1 = CCMenu::create(set, NULL);
	pMenu1->setPosition(CCPointZero);
	this->addChild(pMenu1, 1);*/
	/////////////////////////////////////////////////////



	//	CCParticleSystem* particleSystem = CCParticleFire::create();
	//particleSystem->initWithTotalParticles(90000);

	CCParticleSystem* particleSystem = CCParticleRain::create();
	//	particleSystem->setTexture(CCTextureCache::sharedTextureCache()->addImage("2.png"));
	particleSystem->setPosition(ccp(size.width / 2, size.height));
	particleSystem->setStartSize(2.0f);
	addChild(particleSystem);
	score = CCLabelTTF::create(str_1, "Arial", 25);
	score->setPosition(ccp(0 + 230, size.height - 17));
	this->addChild(score);

	//	lable->runAction(go);

	life_xy[0].x = 20;
	life_xy[0].y = 670;

	life_xy[1].x = 45;
	life_xy[1].y = 670;

	life_xy[2].x = 70;
	life_xy[2].y = 670;

	life_left = 4;



	life[0] = CCSprite::create("data\\life.png");
	life[1] = CCSprite::create("data\\life.png");
	life[2] = CCSprite::create("data\\life.png");



	this->addChild(life[0],10);
	life[0]->setPosition(life_xy[0]);

	this->addChild(life[1],10);
	life[1]->setPosition(life_xy[1]);
	

	this->addChild(life[2], 10);
	life[2]->setPosition(life_xy[2]);

	return true;
}

void scene_1::close(CCObject* pSender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
	CCDirector::sharedDirector()->pushScene(
		CCTransitionSlideInR::create(0.15f, menu::scene())
		);


}

void scene_1::over()
{
	isover = true;
	this->removeChild(plane);
	load_plane();
	switch (life_left)
	{
	case -1:
	{
		this->removeChild(life[0]);
		CCDirector::sharedDirector()->replaceScene(
			CCTransitionSlideInT::create(1.0f, Over::scene())
			);


		/*
		CCSprite* logo_ = CCSprite::create("data\\over.png");
		logo_->setPosition(ccp(size.width / 2, size.height / 2 + 100));

		this->addChild(logo_, 0);
		CCJumpBy *go_ = CCJumpBy::create(2.0f, ccp(0, 0), 00, 2);
		logo_->runAction(go_);

		CCCallFunc*call_ = CCCallFunc::create(this, callfunc_selector(scene_1::over_));
		CCSequence*go_3 = CCSequence::create(go_, call_, NULL);
		this->runAction(go_3);*/

	}
		break;
	case 1:
		this->removeChild(life[1]);
		break;
	case 2:
		this->removeChild(life[2]);
		break;

	case 3:
	
		break;

	default:	
		break;
	}


	/*else
	{

	CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
	CCDirector::sharedDirector()->replaceScene(
	CCTransitionSlideInB::create(0.4f, scene_1::scene())
	);

	}*/

}
